package net.comm.util;

/**
 * <p>Title: XML处理辅助工具类</p>
 * <p>Description: XML处理辅助工具类</p>
 * <p>Copyright: Copyright (c) 2006</p>
 * <p>Company: Long Group</p>
 * @author Christopher He
 * @date 2006-06-30
 * @version 1.0
 */

import java.util.List;
import java.util.Vector;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class FormatUnit {

	private static FormatUnit fu = new FormatUnit();

	private static Log logger = LogFactory.getLog(FormatUnit.class);

	public final static FormatUnit getInstance() {
		return fu;
	}

	// 将二维数组行列颠倒，将行变为列，将列变为行
	public String[][] rowColToExchange(String[][] dualStr) {
		String[][] returnDualStr = null;
		if (dualStr != null) {
			returnDualStr = new String[dualStr[0].length][dualStr.length];
			for (int i = 0; i < dualStr.length; i++)
				for (int j = 0; j < dualStr[0].length; j++)
					returnDualStr[j][i] = dualStr[i][j];
		}
		return returnDualStr;
	}

	// 若字符串是null或者"!"就返回"",否则返回原字符串
	public String format(String data) {
		if (data == null || "!".equals(data)) {
			data = "";
		}
		return data;
	}

	// 将字符串缩小到指定长度，多余的部分用...表示
	public String getStringByLimitLength(String data, int length) {
		if (data == null) {
			return "";
		}
		if (data.length() > length) {
			data = String.valueOf(String.valueOf(data.substring(0, length)))
					.concat("...");
		}
		return data;
	}

	// 将int变量转换为指定长度的字符串，不足部分在前面补0
	public String GetStringByLength(int number, int length) {
		try {
			String tmp = Integer.toString(number);
			for (int i = tmp.length(); i < length; i++) {
				tmp = "0" + tmp;
			}
			return tmp;
		} catch (Exception e) {
			logger.error(e);
			e.printStackTrace();
		}
		return null;
	}

	// 将字符串按照指定长度截取成数组
	public List getStringListByLength(String data, int length) {
		List result = new Vector();
		while (data.length() > length) {
			result.add(data.substring(0, length));
			data = data.substring(length);
		}
		result.add(data);
		return result;
	}

	// 将字符串转换为double型，若不能转化则返回0.0
	public double parseDouble(String data) {
		double result = 0;
		try {
			if (!"".equals(data)) {
				result = Double.parseDouble(data);
			}
		} catch (Exception e) {
			logger.error(e);
			e.printStackTrace();
		}
		return result;
	}

	// 将字符串转换为float型，若不能转化则返回0.0
	public double parseFloat(String data) {
		float result = 0;
		try {
			if (!"".equals(data)) {
				result = Float.parseFloat(data);
			}
		} catch (Exception e) {
			logger.error(e);
			e.printStackTrace();
		}
		return result;
	}

	// 将字符串转换为int型，若不能转化则返回0
	public int parseInt(String data) {
		int result = 0;
		try {
			if (!"".equals(data)) {
				result = Integer.parseInt(data);
			}
		} catch (Exception e) {
			logger.error(e);
			e.printStackTrace();
		}
		return result;
	}

	// 将字符串转换为long型，若不能转化则返回0
	public long parseLong(String data) {
		long result = 0;
		try {
			if (!"".equals(data)) {
				result = Long.parseLong(data);
			}
		} catch (Exception e) {
			logger.error(e);
			e.printStackTrace();
		}
		return result;
	}

	public static void main(String[] arg) {
		int number = 3;
		System.out.println(FormatUnit.getInstance()
				.GetStringByLength(number, 3));
	}

}
